package main

import (
	"encoding/json"
	"fmt"

	"google.golang.org/protobuf/proto"
)

func main() {
	fmt.Println("A test for converting proto to json.")
	var oRpd *PhtList_Respond = new(PhtList_Respond)
	oRpd.Code = 0x3001
	oRpd.Command = "Usr_Pht_List"
	oRpd.Result = "0"
	oRpd.Message = "成功"
	//var oItm *PhtList_Item = &PhtList_Item{}
	var oItm *PhtList_Item = new(PhtList_Item)
	oItm.PhtIdntifr = "1"
	oItm.PhtStatus = "1"
	oItm.UrlFinalztion = "https://www.baidu.com/1.jpg"
	oRpd.List = append(oRpd.List, oItm)

	sByt, _ := json.Marshal(oRpd)
	fmt.Println("PhtList_Respond is: " + string(sByt))

	aBfr, _ := proto.Marshal(oRpd)
	nBfr := len(aBfr)
	fmt.Println("data is: ", nBfr)
	var oPkg *MsgCommon_Respond = new(MsgCommon_Respond)
	proto.Unmarshal(aBfr, oPkg)

	oRpd = nil
	oRpd = new(PhtList_Respond)

	switch oPkg.Code {
	case 0x3001:
		{
			proto.Unmarshal(aBfr, oRpd)
		}
	}
	oRpd = nil
	oRpd = new(PhtList_Respond)

	var sTxt string = "{\"code\":12289,\"command\":\"Usr_Pht_List\",\"result\":\"0\",\"message\":\"成功\",\"list\":[{\"pht_idntifr\":\"1\",\"pht_status\":\"1\",\"url_finalztion\":\"https://www.baidu.com/1.jpg\"}]}"
	json.Unmarshal([]byte(sTxt), oRpd)

	oRpd = nil
}
